<?php
/**
 * Description of generate
 *
 * @author Андрей
 */
class Controller_Generator extends Controller_Template
{
    protected $gen;
    protected $mdirs;

    public function before() 
    {
        parent::before();

        $this->gen = Generator::factory('Mysql');
        $this->mdirs = $this->gen->create_module(Kohana::config('app.module'));
    }

    public function action_do()
    {
        $btn_crud = Arr::get($_POST, 'btn_crud', '');
        
        $model_name = Arr::get($_POST, 'model_name');
        $model_fields = Arr::get($_POST, 'model_fields');

        $fields = $this->gen->parse_fields($model_fields);

        $this->create_model_sql($model_name, $fields);
        $this->create_model_tpl($model_name, $fields);

        if ($btn_crud != '')
        {
            $this->create_crud($model_name, $fields);
        }

        Request::instance()->redirect('index/index');
    }

    public function create_crud($model_name, $fields)
    {
        $dir = Kohana::config('app.folder');
        $set_dir = ($dir != '');

        $data = array(
            'model' => Inflector::singular($model_name),
            'models' => Inflector::plural($model_name),
            'fields' => $fields,
            'set_dir' => $set_dir,
            'dir'   => $dir,
        );

        $controller_tpl = View::factory('templates/controller', $data)->render();
        $file_name = $this->mdirs['generator']
                . DIRECTORY_SEPARATOR . 'controller' . DIRECTORY_SEPARATOR
                . ($set_dir ? $dir.DIRECTORY_SEPARATOR:'')
                . $model_name . '.php';
        $this->gen->save_file($file_name, $controller_tpl);

        $controller_tpl = View::factory('templates/controller_ext', $data)->render();
        $file_name = $this->mdirs['controller'] . $model_name . '.php';
        $this->gen->save_file($file_name, $controller_tpl);

        $index_tpl = View::factory('templates/view_index', $data)->render();
        $file_name = $this->mdirs['views'] . $model_name. DIRECTORY_SEPARATOR . 'index.php';
        $this->gen->save_file($file_name, $index_tpl);

        $edit_tpl = View::factory('templates/view_edit', $data)->render();
        $file_name = $this->mdirs['views'] . $model_name. DIRECTORY_SEPARATOR . 'edit.php';
        $this->gen->save_file($file_name, $edit_tpl);

        $new_tpl = View::factory('templates/view_new', $data)->render();
        $file_name = $this->mdirs['views'] . $model_name. DIRECTORY_SEPARATOR . 'new.php';
        $this->gen->save_file($file_name, $new_tpl);

        $form_tpl = View::factory('templates/view_form', $data)->render();
        $file_name = $this->mdirs['views'] . $model_name. DIRECTORY_SEPARATOR . '_form.php';
        $this->gen->save_file($file_name, $form_tpl);
    }

    public function create_model_tpl($model_name, $fields)
    {
        $data = array(
            'table' => $model_name,
            'fields' => $fields,
        );

        $model_tpl = View::factory('templates/model', $data)->render();
        $this->gen->save_file($this->mdirs['generator']
                . DIRECTORY_SEPARATOR . 'model' . DIRECTORY_SEPARATOR 
                . Inflector::singular($model_name). '.php', $model_tpl);

        $model_tpl = View::factory('templates/model_ext', $data)->render();
        $this->gen->save_file($this->mdirs['model'] . Inflector::singular($model_name). '.php', $model_tpl);
    }

    public function create_model_sql($model_name, $fields)
    {                
        $sql = View::factory('mysql/create_table_sql', array(
            'table' => Inflector::plural($model_name),
            'fields' => $fields,
        ))->render();

        DB::query(Database::UPDATE, $sql)->execute();

        $this->gen->save_file($this->mdirs['module'] .'sql'. DIRECTORY_SEPARATOR
                                . 'create_table_' . Inflector::plural($model_name) . '.sql', $sql);

        foreach ($fields as $f)
        {
            if ($f['ref'] != '')
            {
                $sql = View::factory('mysql/create_ref_sql', array(
                    'table_from' => Inflector::plural($model_name),
                    'field' => $f['name'],
                    'table_to' => Inflector::plural($f['ref']),
                ))->render();

                DB::query(Database::UPDATE, $sql)->execute();

                $this->gen->save_file($this->mdirs['module'] .'sql'. DIRECTORY_SEPARATOR
                . 'create_index_' . Inflector::plural($model_name). '_' . $f['name'] . '.sql', $sql);
            }
        }
    }
}
